import 'package:app/pages/home_module/home_view.dart';
import 'package:app/utils/persisten_storage.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import '../common/colors.dart';
import '../components/keep_alive_wrapper.dart';
import '../components/my_webview.dart';
import 'contact_module/contact_page.dart';
import 'cooperate_modules/cooperate_view.dart';
import 'mine_module/mine_page.dart';
import 'package:get/get.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

class MyChromeSafariBrowser extends ChromeSafariBrowser {
  @override
  void onOpened() {
    print("ChromeSafari browser opened");
  }

  // @override
  // void onCompletedInitialLoad(didLoadSuccessfully) {
  //   print("ChromeSafari browser initial load completed");
  // }

  @override
  void onClosed() {
    print("ChromeSafari browser closed");
  }
}


var _lastTime;

Future<bool> _isExit()async {
  if (_lastTime == null ||
      DateTime.now().difference(_lastTime) > Duration(seconds: 2)) {
    _lastTime = DateTime.now();
    print("再次点击退出应用");
    BotToast.showText(text: '再按一次退出应用');
    return Future.value(false);
  }
  return Future.value(true);
}



class Tabs extends StatefulWidget{
  const Tabs({super.key});

  @override
  State<StatefulWidget> createState() {
    return TabState();
  }
}

class TabState extends State<Tabs>{

  int _currentIndex = 0;

  final ChromeSafariBrowser browser = MyChromeSafariBrowser();


  final pageController = PageController();

  void onTap(int index) {

    print(index);
    if(index == 2){
      return;
    }
    pageController.jumpToPage(index);
  }

  void onPageChanged(int index) {
    setState(() {
      _currentIndex = index;
    });
  }


  final List<Widget> _listPageData = [
    // const KeepAliveWrapper(child: HomeConver()),
    KeepAliveWrapper(child: CooperateView()),

    const KeepAliveWrapper(child: ContactPage()),
    Container(),
    KeepAliveWrapper(child: HomeView()),
    // const KeepAliveWrapper(child: ContactPage()),
    KeepAliveWrapper(child: MinePage(),),
  ];
  @override


  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop:_isExit,
    child:Scaffold(
      // body: _listPageData[_currentIndex],
      // body: bodyList[currentIndex],

      body: PageView(
        controller: pageController,
        onPageChanged: onPageChanged,
        physics: const NeverScrollableScrollPhysics(),
        children: _listPageData, // 禁止滑动
      ),
      bottomNavigationBar:
      BottomNavigationBar(
        currentIndex: this._currentIndex,//配置对应的索引值选中
        onTap: onTap,
        iconSize: 20.0,//icon的大小
        fixedColor: AppColor.themeColor,//选中颜色
        selectedFontSize: 12,
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(
            icon: Image.asset('images/ic_xiezuo.png',width: 24,height: 24,),
            activeIcon: Image.asset('images/ic_xiezuo_sel.png',width: 24,height: 24,),
            label: '协作',
          ),

          BottomNavigationBarItem(
              icon: Image.asset('images/ic_tongxunlu.png',width: 24,height: 24,),
              activeIcon: Image.asset('images/ic_tongxunlu_sel.png',width: 24,height: 24,),
              label: '通讯录'
          ),

          BottomNavigationBarItem(
              icon: Image.asset('images/tongxunlu_icon.png',width: 25,height: 25,),
              activeIcon: Image.asset('images/tongxunlu_icon.png',width: 25,height: 25,color:AppColor.themeColor,),

              label: ''
          ),

          BottomNavigationBarItem(
            icon: Image.asset('images/ic_app.png',width: 24,height: 24,),
            activeIcon: Image.asset('images/ic_app_sel.png',width: 24,height: 24,),
            label: '应用',
          ),


          // BottomNavigationBarItem(
          //     icon: Icon(Icons.add),
          //     label: 'Text("添加")'
          // ),
          BottomNavigationBarItem(
              icon: Image.asset('images/ic_mine.png',width: 24,height: 24,),
              activeIcon: Image.asset('images/ic_mine_sel.png',width: 24,height: 24,),
              label: '我的'
          ),
        ],
      ),
      floatingActionButton: GestureDetector(
        onTap: ()async{
          print('mid=========${await PersistentStorage().getStorage('dtcloud_login_url')}');
          await browser.open(
            url:WebUri(await PersistentStorage().getStorage('dtcloud_login_url')),
          );
          // Get.to(MyWebView('','${await PersistentStorage().getStorage('dtcloud_login_url')}'));
        },
        child: Container(//Container框
          height: 55,
          width: 55,
          decoration: BoxDecoration(//实现圆
            borderRadius: BorderRadius.circular(40),
            color: Colors.white,
          ),
          margin: EdgeInsets.only(top: 35),
          child: Image.asset('images/bottom_log_icon.png',fit: BoxFit.contain,),
          // margin: EdgeInsets.only(top:30,left: 15,right: 15),//外部margin
          // padding: EdgeInsets.all(18), //内部margin
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,//Floating放在底部中间
    ),);



  }
}